<template>
  <div class="empty">
    <div style="height: 175px;width: 124px;position: absolute;top: 35%;left: 50%;margin-left: -62px">
      <img
        src="/static/images/1dd.png"
        style="height: 70px;width: 90px;;margin-left: 17px"
      />
      <img
        src="/static/images/load.gif"
        style="height: 105px;width: 124px;opacity:0.5;"
      />
    </div>
    <!--
    检测用户状态
    <p>openid = {{ openid }}</p>
    <p>{{ msg }}</p>-->
    <mytoast ref="toast" />
  </div>
</template>

<script>
import api from "@/utils/api";
import mytoast from "@/components/toast";

export default {
  components: { mytoast },

  data() {
    return {
      msg: "",
      openid: ""
    };
  },

  created() {},
  onShow: function() {
    var _this = this;
    wx.login({
      success(res) {
        console.log(res);
        if (res.code) {
          //获取openId
          wx.request({
            url: api.getOpenId,
            data: {
              JsCode: res.code
            },
            success(res) {
              try {
                console.log(res);
                var data = res.data;
                 var openid = JSON.parse(data.data).openid;
                 //var openid = "oOTGm5Je5jP3MBE6DitQUPWQU52w";
                _this.openid = openid;

                console.log("openId = " + res);
                wx.setStorageSync("openId", openid);
                //检查用户权限
                wx.request({
                  url: api.checkUser,
                  data: {
                    openId: openid
                  },
                  success(res) {
                    console.log("checkUser=" + res);
                    _this.msg = JSON.stringify(res);
                    if (res.data.data.isBind) {
                      //绑定账号
                      //  _this.goMenu();
                        if(res.data.data.registerSource == '00'){
                            wx.setStorageSync("otherPath",api.path00);
                            wx.setStorageSync("sessionId", res.data.data.sessionId);
                            wx.setStorageSync("fslb", res.data.data.fslb);
                            _this.goMenu();
                        }else if(res.data.data.registerSource == '16'){
                            wx.setStorageSync("otherPath",api.path16);
                            wx.request({
                              url: wx.getStorageSync("otherPath") + api.checkUser2,
                              data: {
                                openId: openid
                              },
                              success(res) {
                                _this.msg = JSON.stringify(res);
                                if (res.data.data.isBind) {
                                  //绑定账号
                                    if(res.data.data.registerSource == '16'){
                                      _this.goMenu();
                                    }
                                    wx.setStorageSync("sessionId", res.data.data.sessionId);
                                    wx.setStorageSync("fslb", res.data.data.fslb);
                                } else {
                                  //未绑定
                                  _this.goGuid();
                                }

                              }
                            });
                        }
                    } else {
                      //未绑定
                      _this.goGuid();
                    }

                  }
                });
              } catch (e) {}
            }
          });
        } else {
          console.log("登录失败！" + res.errMsg);
        }
      },
      fail() {
        _this.$refs.toast.show("网络异常");
      },
      timeout() {
        _this.$refs.toast.show("网络异常");
      }
    });
  },
  methods: {
    goMenu() {
      var url = "../menu/main";
      wx.navigateTo({ url });
    },
    goGuid() {
      var url = "../index/main";
      wx.navigateTo({ url });
    }
  }
};
</script>

<style lang="wxss">
page {
  width: 100%;
  height: 100%;
  background-color: #0077ff;
}
</style>

<style scoped>
.empty {
  width: 100%;
  height: 100%;
  background-color: #0077ff;
}
</style>
